﻿using CharmFlyoutLibrary;
using cnBlogs.Common;
using cnBlogs.Common.Helper;
using System;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Animation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace cnBlogs.Views
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class SplashScreenView
    {
        private SplashScreen splash; // Variable to hold the splash screen object.
        private CharmFrame _rootFrame;
        internal Rect splashImageRect; // Rect to store splash screen image coordinates.
        internal bool dismissed = false; // Variable to track splash screen dismissal status.

        public SplashScreenView(SplashScreen splashscreen, bool loadState)
        {
            this.InitializeComponent();

            // Create a Frame to act as the navigation context and associate it with
            // a SuspensionManager key

            // Listen for window resize events to reposition the extended splash screen image accordingly.
            // This is important to ensure that the extended splash screen is formatted properly in response to snapping, unsnapping, rotation, etc...
            Window.Current.SizeChanged += new WindowSizeChangedEventHandler(ExtendedSplash_OnResize);

            splash = splashscreen;

            if (splash != null)
            {
                // Register an event handler to be executed when the splash screen has been dismissed.
                splash.Dismissed += new TypedEventHandler<SplashScreen, Object>(DismissedEventHandler);

                // Retrieve the window coordinates of the splash screen image.
                splashImageRect = splash.ImageLocation;
                PositionImage();
            }

            // Create a Frame to act as the navigation context 
            _rootFrame = new CharmFrame { CharmContent = new SettingPageHelper() };
            SuspensionManager.RegisterFrame(_rootFrame, "AppFrame");

            RestoreStateAsync(loadState);


        }

        void sb_Completed(object sender, object e)
        {
            if (_rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                if (!_rootFrame.Navigate(typeof(MainPage)))
                {
                    throw new Exception("Failed to create initial page");
                }
            }

            // Place the frame in the current Window and ensure that it is active
            Window.Current.Content = _rootFrame;
        }

        void PositionImage()
        {
            grid_logo.SetValue(Canvas.LeftProperty, splashImageRect.X);
            grid_logo.SetValue(Canvas.TopProperty, splashImageRect.Y);
            img_logo.Height = splashImageRect.Height;
            img_logo.Width = splashImageRect.Width;
        }
        private async void DismissedEventHandler(SplashScreen sender, object args)
        {
            dismissed = true;
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
              {
                  Storyboard sb = this.Resources["FadeOut"] as Storyboard;
                  sb.Completed += sb_Completed;
                  sb.Begin();
              });

        }

        private void ExtendedSplash_OnResize(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            // Safely update the extended splash screen image coordinates. This function will be fired in response to snapping, unsnapping, rotation, etc...
            if (splash != null)
            {
                // Update the coordinates of the splash screen image.
                splashImageRect = splash.ImageLocation;
                PositionImage();
            }
        }
        async void RestoreStateAsync(bool loadState)
        {
            if (loadState)
                await SuspensionManager.RestoreAsync();

            // Normally you should start the time consuming task asynchronously here and 
            // dismiss the extended splash screen in the completed handler of that task
            // This sample dismisses extended splash screen  in the handler for "Learn More" button for demonstration
        }
    }
}
